#!/system/bin/sh

USE_BASE_IMAGE=0

function user_select_url() {
	# DEF_IMG_arm64=0
	# DEF_IMG_amd64=1

	# case "${CURRENT_VM_ARCH}" in
	# 	"amd64")
	# 		DEF_IMG_SEL=$DEF_IMG_amd64
	# 		;;
	# 	*)
	# 		DEF_IMG_SEL=$DEF_IMG_arm64
	# 		;;
	# esac

	# echo "虚拟电脑将rootfs分为以下两类:"
	# echo " 1). 常规系的rootfs https://cdimage.ubuntu.com/ubuntu-base/releases/22.10/release/"
	# echo " 2). lxc 系的rootfs https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/ubuntu/kinetic/"
	# echo "     "
	# echo "第一种体积小运行速度稍快, 但dbus容易出现莫名其妙的问题, dpkg/apt 安装软件的时候也容易出错"
	# echo "请尽量选择lxc-rootfs镜像"
	# echo ""
	# echo ""
	# # echo "请选择rootfs"
	# # echo "输入其它值将取消安装 请选择 [1/2/3]"
	# # read -t 180 readrlt
	# readrlt=${DEF_IMG_SEL}
	# echo "自动化安装不会显示可用的 rootfs 列表."
	# echo "已选择的rootfs_id: |$readrlt|"

	# LINUX_TXT=$(eval echo \$IMAGE_TXT_${readrlt})
	# LINUX_ZIP=$(eval echo \$IMAGE_ZIP_${readrlt})
	# LINUX_URL=$(eval echo \$IMAGE_URL_${readrlt})"/$LINUX_ZIP"
	# UNZIP_ARG=$(eval echo \$UNZIP_OPT_${readrlt})

	# UNZIP_ARG="$UNZIP_ARG"
	# UNZIP_ARG="$UNZIP_ARG -v"



	# # ubuntu 官网镜像，更新频繁，难以汉化
	# LINUX_TXT="ubuntu官网"
	# LINUX_ZIP=ubuntu-base-${LINUX_ROOTFS_VER}-base-${CURRENT_VM_ARCH}.tar.gz
	# LINUX_URL=https://cdimage.ubuntu.com/ubuntu-base/releases/${LINUX_ROOTFS_VER}/release/$LINUX_ZIP
	# UNZIP_ARG=-xzf

	# ## 固定的镜像，用得时间最久
	# LINUX_TXT="虚拟电脑官网"
	# LINUX_ZIP=lxc_ubuntu-base-${LINUX_ROOTFS_VER}-base-${CURRENT_VM_ARCH}.tar.xz
	# LINUX_URL=${APP_URL_DLSERVER}/$LINUX_ZIP
	# UNZIP_ARG=-xJf
}

function get_debug_image_url() {

	LINUX_URL=
	if [ 1 -eq 1 ]; then
		echo "已停用 debug image"
		return
	fi

	LINUX_TXT="本地调试服务器"

	case "${vmDistribution}" in
		"3")
			LINUX_ZIP=deepin-aarch64-pd-v4.0.2.tar.xz
			LINUX_URL=${APP_URL_DLSERVER}/$LINUX_ZIP
			UNZIP_ARG=-xJf

			LINUX_ZIP=deepin-rootfs-arm64.tar.gz
			LINUX_URL=${GITHUB_PROXY_HTTP}https://github.com/deepin-community/deepin-rootfs/releases/download/v1.2.0/$LINUX_ZIP
			UNZIP_ARG=-xf
			
			;;
		"2")
			LINUX_ZIP=lxc_debian-12.04-arm64.tar.gz
			LINUX_URL=${APP_URL_DLSERVER}/$LINUX_ZIP
			UNZIP_ARG=-xzf
			;;
		"1" | *)

			LINUX_URL=
			echo "已停用 debug image"
			return

			if [ 1 -eq 0 ]; then
				LINUX_ZIP=ubuntu-base-${LINUX_ROOTFS_VER}-base-${CURRENT_VM_ARCH}.tar.gz
				LINUX_URL=${APP_URL_DLSERVER}/$LINUX_ZIP
				UNZIP_ARG=-xzf

				# LINUX_ZIP=lxc_ubuntu-base-${LINUX_ROOTFS_VER}-base-${CURRENT_VM_ARCH}.tar.xz
				# LINUX_URL=${APP_URL_DLSERVER}/$LINUX_ZIP
				# UNZIP_ARG=-xJf
			else
				# https://cdimage.ubuntu.com/ubuntu-base/releases/23.10/release/ubuntu-base-23.10-base-arm64.tar.gz
				LINUX_TXT="ubuntu官网"
				LINUX_ZIP=ubuntu-base-${LINUX_ROOTFS_VER}-base-${CURRENT_VM_ARCH}.tar.gz
				LINUX_URL=https://cdimage.ubuntu.com/ubuntu-base/releases/${LINUX_ROOTFS_VER}/release/$LINUX_ZIP
				UNZIP_ARG=-xzf
			fi
			;;
	esac
}


# ========================= 第一类 base-image ================================
function get_url_from_ubuntu() {
	# https://cdimage.ubuntu.com/ubuntu-base/releases/23.10/release/ubuntu-base-23.10-base-arm64.tar.gz
	LINUX_TXT="ubuntu官网"
	LINUX_ZIP=ubuntu-base-${LINUX_ROOTFS_VER}-base-${CURRENT_VM_ARCH}.tar.gz
	LINUX_URL=https://cdimage.ubuntu.com/ubuntu-base/releases/${LINUX_ROOTFS_VER}/release/$LINUX_ZIP
	UNZIP_ARG=-xzf
}

function get_url_from_aliyun() {
	# https://cdimage.ubuntu.com/ubuntu-base/releases/23.10/release/ubuntu-base-23.10-base-arm64.tar.gz
	LINUX_TXT="阿里云"
	LINUX_ZIP=ubuntu-base-${LINUX_ROOTFS_VER}-base-${CURRENT_VM_ARCH}.tar.gz
	LINUX_URL=https://mirrors.aliyun.com/ubuntu-cdimage/ubuntu-base/releases/${LINUX_ROOTFS_VER}/release/$LINUX_ZIP
	#   https://mirrors.aliyun.com/ubuntu-cdimage/ubuntu-base/releases/24.04/release/ubuntu-base-24.04-base-arm64.tar.gz
	UNZIP_ARG=-xzf
}


# ========================= 第二类 lxc-image ================================
function get_url_from_tsinghua() {
	LINUX_TXT="清华大学lxc-image仓库"
	LINUX_ZIP=rootfs.tar.xz
	echo "LINUX_TXT: $LINUX_TXT"
	tmp_base_url=https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/${LINUX_DISTRIBUTION}/${LINUXVersionName}/${CURRENT_VM_ARCH}/default/
	ndkhttpsget ${tmp_base_url} index.html  2>&1
	httpget_rlt=$?
	if [ $httpget_rlt -ne 0 ]; then
		rm -rf ${app_temp}/index.html
	fi
	cp -f ${app_temp}/index.html ./ 2>/dev/null
	DATETIME_FIELD=
	if [ -f index.html ]; then
		DATETIME_FIELD=$(cat index.html |grep 'title="20'|grep 'href="20'|${ALINKER64} "${tools_dir}/busybox/busybox" awk -v FS="\"" '{print $4}'|${ALINKER64} "${tools_dir}/busybox/busybox" head -c 16) 2>&1
	fi
	LINUX_URL=${tmp_base_url}$DATETIME_FIELD/$LINUX_ZIP
	UNZIP_ARG=-xJf

	# tmp_base_url=https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/${LINUX_DISTRIBUTION}/${LINUXVersionName}/${CURRENT_VM_ARCH}/default/
	# ndkhttpsget ${tmp_base_url} index.html  2>&1
	# httpget_rlt=$?
	# if [ $httpget_rlt -ne 0 ]; then
	# 	rm -rf ${app_temp}/index.html
	# fi
	# cp -f ${app_temp}/index.html ./ 2>/dev/null
	# DATETIME_FIELD=
	# if [ -f index.html ]; then
	# 	DATETIME_FIELD=$(cat index.html |grep 'title="20'|grep 'href="20'|${ALINKER64} "${tools_dir}/busybox/busybox" awk -v FS="\"" '{print $4}'|${ALINKER64} "${tools_dir}/busybox/busybox" head -c 16) 2>&1
	# fi
	# LINUX_URL=${tmp_base_url}$DATETIME_FIELD/$LINUX_ZIP
	# UNZIP_ARG=-xJf
}

function get_url_from_bfsu() {
	LINUX_TXT="北京外国语大学lxc-image仓库"
	LINUX_ZIP=rootfs.tar.xz
	echo "LINUX_TXT: $LINUX_TXT"
	tmp_base_url=https://mirrors.bfsu.edu.cn/lxc-images/images/${LINUX_DISTRIBUTION}/${LINUXVersionName}/${CURRENT_VM_ARCH}/default/
	ndkhttpsget ${tmp_base_url} index.html  2>&1
	httpget_rlt=$?
	if [ $httpget_rlt -ne 0 ]; then
		rm -rf ${app_temp}/index.html
	fi
	cp -f ${app_temp}/index.html ./ 2>/dev/null
	DATETIME_FIELD=
	if [ -f index.html ]; then
		DATETIME_FIELD=$(cat index.html |grep 'title="20'|grep 'href="20'|${ALINKER64} "${tools_dir}/busybox/busybox" awk -v FS="\"" '{print $4}'|${ALINKER64} "${tools_dir}/busybox/busybox" head -c 16) 2>&1
	fi
	LINUX_URL=${tmp_base_url}$DATETIME_FIELD/$LINUX_ZIP
	UNZIP_ARG=-xJf
}

function get_url_from_nyist() {
	LINUX_TXT="南阳理工学院lxc-image仓库"
	LINUX_ZIP=rootfs.tar.xz
	echo "LINUX_TXT: $LINUX_TXT"
	tmp_base_url=https://mirror.nyist.edu.cn/lxc-images/images/${LINUX_DISTRIBUTION}/${LINUXVersionName}/${CURRENT_VM_ARCH}/default/
	ndkhttpsget ${tmp_base_url} index.html  2>&1
	httpget_rlt=$?
	if [ $httpget_rlt -ne 0 ]; then
		rm -rf ${app_temp}/index.html
	fi
	cp -f ${app_temp}/index.html ./ 2>/dev/null
	DATETIME_FIELD=
	if [ -f index.html ]; then
		DATETIME_FIELD=$(cat index.html |grep 'title="20'|grep 'href="20'|${ALINKER64} "${tools_dir}/busybox/busybox" awk -v FS="\"" '{print $4}'|${ALINKER64} "${tools_dir}/busybox/busybox" head -c 16) 2>&1
	fi
	LINUX_URL=${tmp_base_url}$DATETIME_FIELD/$LINUX_ZIP
	UNZIP_ARG=-xJf
}

function get_url_from_iscas() {
	LINUX_TXT="中国科学院软件研究所lxc-image仓库"
	LINUX_ZIP=rootfs.tar.xz
	echo "LINUX_TXT: $LINUX_TXT"
	tmp_base_url=https://mirror.iscas.ac.cn/lxc-images/images/${LINUX_DISTRIBUTION}/${LINUXVersionName}/${CURRENT_VM_ARCH}/default/
	ndkhttpsget ${tmp_base_url} index.html  2>&1
	httpget_rlt=$?
	if [ $httpget_rlt -ne 0 ]; then
		rm -rf ${app_temp}/index.html
	fi
	cp -f ${app_temp}/index.html ./ 2>/dev/null
	DATETIME_FIELD=
	if [ -f index.html ]; then
		DATETIME_FIELD=$(cat index.html |grep 'title="20'|grep 'href="20'|${ALINKER64} "${tools_dir}/busybox/busybox" awk -v FS="\"" '{print $6}'|${ALINKER64} "${tools_dir}/busybox/busybox" head -c 16) 2>&1
	fi
	LINUX_URL=${tmp_base_url}$DATETIME_FIELD/$LINUX_ZIP
	UNZIP_ARG=-xJf
}

function get_url_from_nju() {
	LINUX_TXT="南京大学lxc-image仓库"
	LINUX_ZIP=rootfs.tar.xz
	echo "LINUX_TXT: $LINUX_TXT"
	tmp_base_url=https://mirror.nju.edu.cn/lxc-images/images/${LINUX_DISTRIBUTION}/${LINUXVersionName}/${CURRENT_VM_ARCH}/default/
	ndkhttpsget ${tmp_base_url} index.html  2>&1
	httpget_rlt=$?
	if [ $httpget_rlt -ne 0 ]; then
		rm -rf ${app_temp}/index.html
	fi
	cp -f ${app_temp}/index.html ./ 2>/dev/null
	DATETIME_FIELD=
	if [ -f index.html ]; then
		DATETIME_FIELD=$(cat index.html |grep 'title="20'|grep 'href="20'|${ALINKER64} "${tools_dir}/busybox/busybox" awk -v FS="\"" '{print $6}'|${ALINKER64} "${tools_dir}/busybox/busybox" head -c 16) 2>&1
	fi
	LINUX_URL=${tmp_base_url}$DATETIME_FIELD/$LINUX_ZIP
	UNZIP_ARG=-xJf
}

get_prebuild_bakimg

if [ $USE_BASE_IMAGE -eq 1 ]; then

	# 阿里云 镜像站点
	if [ ! -f $LINUX_ZIP ]; then
		get_url_from_aliyun
		do_donwload_rootfs
	fi

	# ubuntu 官方网站
	if [ ! -f $LINUX_ZIP ]; then
		get_url_from_ubuntu
		do_donwload_rootfs
	fi

else

	# 清华大学 lxc-image 镜像站点
	if [ ! -f $LINUX_ZIP ]; then
		get_url_from_tsinghua
		do_donwload_rootfs
	fi

	# 北京外国语大学 lxc-image 镜像站点
	if [ ! -f $LINUX_ZIP ]; then
		get_url_from_bfsu
		do_donwload_rootfs
	fi

	# 中国科学院软件研究所lxc-image仓库
	if [ ! -f $LINUX_ZIP ]; then
		get_url_from_iscas
		do_donwload_rootfs
	fi

	# 南京大学lxc-image仓库
	if [ ! -f $LINUX_ZIP ]; then
		get_url_from_nju
		do_donwload_rootfs
	fi

	# 南阳理工学院lxc-image仓库
	if [ ! -f $LINUX_ZIP ]; then
		get_url_from_nyist
		do_donwload_rootfs
	fi
fi